package com.bean.keeper;

import com.annotation.healthStatus;
import com.annotation.sleepStatus;
import com.bean.FlyAnimalImpl.FlyAnimal;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Cc
 * 2024-04-06
 */
public class FlyAnimalKeeper<F extends FlyAnimal> extends Keeper {
    private ArrayList<F> flyAnimal = new ArrayList<>();

    public void addAnimal(List<F> animal) {
        this.flyAnimal = (ArrayList<F>) animal;
    }

    /**
     * 喂养
     *
     * @throws NoSuchMethodException     没有这样方法例外
     * @throws InvocationTargetException 调用目标异常
     * @throws IllegalAccessException    非法访问例外
     */
    @Override
    public void feeding() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        for (F a : flyAnimal) {
            System.out.println("飞禽动物饲养员喂" + a.getClass().getSimpleName());

            Method declaredMethod = a.getClass().getDeclaredMethod("eat");
            declaredMethod.invoke(a);
        }
    }

    /**
     * 检查
     */
    @Override
    public void inspection() {
        for (F a : flyAnimal) {
            try {
                Method eatMethod = a.getClass().getDeclaredMethod("sleep");
                if (eatMethod.isAnnotationPresent(sleepStatus.class)) {
                    sleepStatus annotation = eatMethod.getAnnotation(sleepStatus.class);
                    String value = annotation.value();
                    if ("差".equals(value)) {
                        System.out.println("饲养员配发安眠药给" + a.getClass().getSimpleName());
                    } else {
                        System.out.println(a.getClass().getSimpleName() + "睡得嘛香");
                    }
                }
            } catch (NoSuchMethodException e) {

            }
        }
    }

    /**
     * 体检
     */
    @Override
    public void physicalExamination() {
        for (F a : flyAnimal) {
            if (a.getClass().isAnnotationPresent(healthStatus.class)) {
                healthStatus annotation = a.getClass().getAnnotation(healthStatus.class);
                String value = annotation.value();
                if ("差".equals(value)) {
                    System.out.println("饲养员特别照看" + a.getClass().getSimpleName());
                } else {
                    System.out.println(a.getClass().getSimpleName() + "身体倍棒");
                }
            }
        }
    }
}
